/**
 * Created by Administrator on 2015/12/14.
 */

var setting = {
    view: {
        showIcon: function (treeId, treeNode) {
            return !treeNode.isParent;
        }
    }, data: {
        simpleData: {
            enable: true
        }
    }, callback: {onClick: zTreeOnClick}
};


function zTreeOnClick(event, treeId, treeNode) {
    baseDic.default_table = treeNode.Ename;
    baseDic.default_pid = treeNode.id;
    jqGrid.init();

}
var baseDic = {
    default_table: '', default_pid: '', leftTree: function (parentId) {
        $.post($basePath + "/sup/basedic/queryBaseTable", {parentid: parentId}, function (data) {
            var zNodes = new Array();
            var firstId = "";
            for (var i = 0; i < data.length; i++) {
                var tree = new Object();
                if (i < 1) {
                    firstId = data[i].indx;
                }
                tree.id = data[i].indx;
                tree.name = data[i].dbCnname;
                tree.Ename = data[i].dbEnname;
                zNodes.push(tree);
            }
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var node = treeObj.getNodeByParam("id", firstId);
            treeObj.selectNode(node);
            treeObj.setting.callback.onClick(null, "#treeDemo", node);//调用事件

        });
    }
}
var jqGrid = {
    colNamesArr: new Array(), colModelArr: new Array(), getColArr: function () {
        $("#jqGridDiv").html("<table id=\"corptable\"></table><div id=\"jqGridPager\"></div>");
        $.ajax({
            url: $basePath + "/sup/basedic/queryBaseColumns", data: {pIndx: baseDic.default_pid}, async: false, // 注意此处需要同步，因为返回完数据后，下面才能让结果的第一条selected
            type: "POST", dataType: "json", success: function (fields) {
                jqGrid.colNamesArr = [];
                jqGrid.colModelArr = [];
                if (fields) {
                    var colModel = new Array();
                    for (var i = 0; i < fields.length; i++) {
                        colModel = [];
                        jqGrid.colNamesArr.push(fields[i].caption);
                        colModel.name = fields[i].textField;
                        colModel.indx = fields[i].textField;
                        colModel.align = "center";
                        colModel.editable = true;
                        jqGrid.colModelArr.push(colModel);
                    }
                    colModel = [];
                    jqGrid.colNamesArr.push('助记码');
                    colModel.name = 'memCode';
                    colModel.indx = 'memCode';
                    colModel.align = "center";
                    jqGrid.colModelArr.push(colModel);
                    jqGrid.colNamesArr.push('是否失效');
                    colModel = [];
                    colModel.name = 'isEnabled';
                    colModel.indx = 'isEnabled';
                    colModel.align = "center";
                    colModel.formatter = jqGrid.isEnable;
                    jqGrid.colModelArr.push(colModel);
                    jqGrid.colNamesArr.push('主键');
                    colModel = [];
                    colModel.name = 'indx';
                    colModel.indx = 'indx';
                    colModel.hidden = true;
                    jqGrid.colModelArr.push(colModel);
                }
            }
        });
    }, init: function () {
        jqGrid.getColArr();
        jqGrid.jqGridData();
        $("#corptable").setGridWidth($(window).width() * 0.72);
    }, jqGridData: function () {
        $("#corptable").jqGrid({
            datatype: "json",
            url: $basePath + "/sup/basedic/queryBaseColumnVals",
            postData: {pIndx: baseDic.default_pid}, //发送数据
            colNames: jqGrid.colNamesArr,
            colModel: jqGrid.colModelArr,
            rowList: [10, 20, 100], //可调整每页显示的记录数
            viewrecords: true,
            height: 'auto',
            autoWidth: true,
            shrinkToFit: true,
            autoScroll: true,
            rownumbers: true,
            rownumWidth: 35,
            multiselect: true, //是否支持多选
            pager: "#jqGridPager", //分页工具栏
            altRows: true,
            altclass: 'someClass',
            sortable: false,
            loadComplete: function () {
                $("[name='isEnableDiv']").bootstrapSwitch();
                var rowNum = $(this).jqGrid('getGridParam', 'records');
                if (rowNum <= 0) {
                    if ($(this).parent().children('.norecords').length == 0)
                        $(this).parent().append("<div class=\"norecords\">暂无数据！</div>");
                    $(this).parent().children('.norecords').show();
                } else {//如果存在记录，则隐藏提示信息。
                    $(this).parent().children('.norecords').hide();
                }
            }
            //, onSelectRow: function (id) {
            //    if (id && id !== lastsel) {
            //        $('#corptable').jqGrid('restoreRow', lastsel);
            //        $('#corptable').jqGrid('editRow', id, true);
            //        lastsel = id;
            //    }
            //}, editurl: $basePath + "/basedic/updateBaseColumnVals?table=" + baseDic.default_table

        });
    }, navJqGrid: function () {
        $("#corptable").jqGrid('navGrid', "#corptable", {
            edit: false, add: false, del: false
        });
    }, isEnable: function (cellvalue, options, rowObject) {
        if ('1' == cellvalue) {
            return "<input type='checkbox' onchange='isEnable(this)' name='isEnableDiv' id='" + rowObject.indx + "' data-size='mini' data-on-text='是' data-off-text='否'>";
        }
        return "<input type='checkbox' onchange='isEnable(this)' name='isEnableDiv' id='" + rowObject.indx + "' checked data-size='mini' data-on-text='是' data-off-text='否'>";
    }
};

$(window).resize(function () {
    $("#corptable").setGridWidth($(window).width() * 0.72);
})
//查询

function isEnable(obj) {
    $.post($basePath + "/sup/basedic/updateEnable", {indx: $(obj).attr("id"), pIndx: baseDic.default_pid}, function () {
            showOKMessage("更新成功!");
    });
}
//$("button[name=add]").click(function () {
//    if ("" == baseDic.default_pid || null == baseDic.default_pid) {
//        ShowMessages("请选择一个字典项后在进行新增", "系统提示", "msgwaring", null, 0);
//        return;
//    }
//    window.location.href = $basePath + "/sup/basedic/addBase?pIndx=" + baseDic.default_pid;
//});
function btnEdit() {
    var jsonArr = getId("corptable");
    if (1 < jsonArr.length) {
        ShowMessages("对不起,您只能选中一条记录", "系统提示", "msgwaring", null, 0);
    } else if (0 == jsonArr.length) {
        ShowMessages("对不起,请选择需要修改的记录", "系统提示", "msgwaring", null, 0);
    } else {
        window.location.href = $basePath + "/sup/basedic/editBase?indx=" + jsonArr[0].indx + "&pIndx=" + baseDic.default_pid;
    }
}
function btnEditBus() {
    var jsonArr = getId("corptable");
    if (1 < jsonArr.length) {
        ShowMessages("对不起,您只能选中一条记录", "系统提示", "msgwaring", null, 0);
    } else if (0 == jsonArr.length) {
        ShowMessages("对不起,请选择需要修改的记录", "系统提示", "msgwaring", null, 0);
    } else {
        window.location.href = $basePath + "/sup/basedic/editBusiness?indx=" + jsonArr[0].indx + "&pIndx=" + baseDic.default_pid;
    }
}

function btnSearch() {
    $("#corptable").jqGrid('setGridParam', {
        url: $basePath + "/sup/basedic/queryBaseColumnVals",
        postData: {pIndx: baseDic.default_pid, valIn: $("#searchVal").val()},
        page: 1
    }).trigger("reloadGrid"); //重新载入
}
